package com.huiminpay.transaction.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.huiminpay.common.cache.util.QRCodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName : PayTestController
只写这个即可
 */
@Slf4j
@Controller
public class PayTestController {
    String APP_ID = "9021000132694605";
    String APP_PRIVATE_KEY ="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDDSBSbpJb2gZzmQUv+ZM1F8thRMX2WER42LCAV8xSIsRJZbKrAx9gNkvCFIAvKeu5WAajMhc/5IE1E6m0opTR/4NS3krOc9+M/OKhyPA7oYxAiAI1YN8xYLmOYxNF7ujqF8sfKumwfPlfsZDH/nwGGr4ao7Ow8rdERooJiAWgQi+3e/QxQyvu4YggwgmENH1hAWfhN7oCd5BOoOHjsu1dxcP5RxwjJHgvakij2CH/yM8H//W8UqlLSNbnbDfV/Ovf0J0oCzX04j+hSo7ZteQ8bf3gZupojOYPDaBc/rTnvhb7COQyXjElJ9EHI8icbhUAdxESpj9zoQeNQVUpOieEnAgMBAAECggEAWlVBky3z11vlpG2YJbmjazQG9KgcHAsXGqAWx5CGcyTWBB/MbRkEzGKoeCx2x9dN4OMrnlBYADn9BGAc1qx1OKJcQ/7Mr1PL/zbKVO2LR9xrXg87Uw+cqz3qPITvEnj3EXdZDGJoGZvRODyxFDoNjqwrcoEGVpthFvNBtOxJoz0IJPT1pOf3UO4ahKUZzbHrdH/xxTYneHM/EkxzWk5e9HTMF8yGtF/iyEmw7BCaxCedxu3mj0OQaYd53nIimh6vZPaqDMqxRmrNZ48EkxK3ZEbX98fXkHvIhxSkpScgv9RriAGKFLfUZgtc3JOWIG1jBB1t1fyTLmUqDSgN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1Vk3hQ5wQUYuAZaHM1pYfz6zAkcKXoBp09UPoE+qmpEHiQdykj3ZRnYcYC1JIUTafIsfyHS5Af+YR2UjhYKJJ3qUcEMuuf56nzKxJeQM4wq26o5HOYLlFootHqnKj1EBu+OZlHEZ37YHTjatevlgbhLsl6En2kf1SAbrNRVrDlgDYgM5AAGq9eMj9uu4p6CcCAVDXDw24scInT4aVv8QIDAQAB";
    String charset = "utf-8";
    String serverUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";//正式"https://openapi.alipay.com/gateway.do"

    @GetMapping("/alipaytest")
    public void alipaytest(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, APP_ID,
                APP_PRIVATE_KEY, "json", charset, ALIPAY_PUBLIC_KEY, "RSA2");

        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();			//创建API对应的request

        alipayRequest.setBizContent("{" +
                " \"out_trade_no\":\"20150320010101123\"," +
                " \"total_amount\":\"0.01\"," +
                " \"subject\":\"Iphone6 16G\"," +
                " \"product_code\":\"QUICK_WAP_PAY\"" +
                " }");//填充业务参数
        String form="";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }


        httpResponse.setContentType("text/html;charset=" + charset);
        httpResponse.getWriter().write(form);//直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }

    public static void main(String[] args) throws IOException {
        QRCodeUtil qrCodeUtil = new QRCodeUtil();
        System.out.println(qrCodeUtil.createQRCode("http://172.16.7.130:56050/transaction/alipaytest",
                200, 200));
    }
}